/*
 * GroupBean.java
 * 
 * Copyright (c) 2011 Administrator. 
 * 
 * This file is part of RMS.
 * 
 * RMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RMS.  If not, see <http ://www.gnu.org/licenses/>.
 */
package com.itrustsolution.rms.managedbean;

import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.RowSelectorEvent;
import com.itrustsolution.rms.entities.RmsGroup;
import com.itrustsolution.rms.util.HibernateUtil;
import com.itrustsolution.rms.util.JSFUtil;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;

/**
 *
 * @author Administrator
 */
public class GroupBean {
    private HtmlInputText inpTxtId;
    private HtmlInputText inpTxtName;
    private HtmlCommandButton cmdBtnAdd;
    private HtmlCommandButton cmdBtnEdit;
    private HtmlCommandButton cmdBtnSave;
    private HtmlCommandButton cmdBtnDelete;    
    private HtmlCommandButton cmdBtnCancel;
    private RmsGroup obj;
    private String result;
    // List of all rms group data
    private List<RmsGroup> dataTables = new ArrayList<RmsGroup>();
    
    /** Creates a new instance of GroupBean */
    public GroupBean() {
    }

    public void add(){
        getInpTxtId().setValue(null);
        getInpTxtId().setSubmittedValue(null);
        getInpTxtName().setValue("");
        getInpTxtName().setSubmittedValue("");
        getInpTxtName().setDisabled(false);
        
        getCmdBtnAdd().setVisible(false);
        getCmdBtnEdit().setVisible(false);
        getCmdBtnSave().setVisible(true);
        getCmdBtnDelete().setVisible(false);
        getCmdBtnCancel().setVisible(true);
        
        getInpTxtName().setFocus(true);
    }
    
    public void edit(){
        getInpTxtName().setFocus(true);
        getInpTxtName().setDisabled(false);
        
        getCmdBtnAdd().setVisible(false);
        getCmdBtnEdit().setVisible(false);
        getCmdBtnSave().setVisible(true);
        getCmdBtnDelete().setVisible(false);
        getCmdBtnCancel().setVisible(true);
    }
    
    public void save(){
        if(getInpTxtId().getValue() == null){
            obj = new RmsGroup();
            result = " created";
        }else{
            obj = (RmsGroup) HibernateUtil.getEntityById(RmsGroup.class, Integer.parseInt((String) getInpTxtId().getValue()));
            result = " edited";
        }
        
        obj.setGroupName(inpTxtName.getValue().toString());
        obj.setGroupIsactive(true);
        HibernateUtil.saveOrUpdate(obj);
        
        JSFUtil.addFacesMessage(FacesMessage.SEVERITY_INFO, getInpTxtName().getValue() + result);
        
        cancel();
    }
    
    public void delete(){
        if (getInpTxtId().getValue() != null) {
            obj = (RmsGroup) HibernateUtil.getEntityById(RmsGroup.class, Integer.parseInt((String) getInpTxtId().getValue()));
            obj.setGroupIsactive(false);
            HibernateUtil.saveOrUpdate(obj);
            
            JSFUtil.addFacesMessage(FacesMessage.SEVERITY_INFO, getInpTxtName().getValue() + " deleted");
        }
    }
    
    public void cancel(){
        getInpTxtId().setValue(null);
        getInpTxtId().setSubmittedValue(null);
        getInpTxtName().setValue("");
        getInpTxtName().setSubmittedValue("");
        
        getInpTxtName().setDisabled(true);
        
        getCmdBtnAdd().setVisible(true);
        getCmdBtnEdit().setVisible(false);
        getCmdBtnSave().setVisible(false);
        getCmdBtnDelete().setVisible(false);
        getCmdBtnCancel().setVisible(false);
    }
    
    public void rowSelectionListener(RowSelectorEvent e) {
        if (!getCmdBtnSave().isVisible()) {
            // Display selected row
            obj = (RmsGroup) getDataTables().get(e.getRow());
            getInpTxtId().setValue(String.valueOf(obj.getGroupId()));
            getInpTxtId().setSubmittedValue(String.valueOf(obj.getGroupId()));
            getInpTxtName().setValue(obj.getGroupName());
            getInpTxtName().setSubmittedValue(obj.getGroupName());
            
            // Show edit and remove button only when row selected
            getCmdBtnEdit().setVisible(true);
            getCmdBtnDelete().setVisible(true);
        }
    }
    
    /**
     * @return the inpTxtId
     */
    public HtmlInputText getInpTxtId() {
        return inpTxtId;
    }

    /**
     * @param inpTxtId the inpTxtId to set
     */
    public void setInpTxtId(HtmlInputText inpTxtId) {
        this.inpTxtId = inpTxtId;
    }
    
    /**
     * @return the inpTxtName
     */
    public HtmlInputText getInpTxtName() {
        return inpTxtName;
    }

    /**
     * @param inpTxtName the inpTxtName to set
     */
    public void setInpTxtName(HtmlInputText inpTxtName) {
        this.inpTxtName = inpTxtName;
    }

    /**
     * @return the cmdBtnAdd
     */
    public HtmlCommandButton getCmdBtnAdd() {
        return cmdBtnAdd;
    }

    /**
     * @param cmdBtnAdd the cmdBtnAdd to set
     */
    public void setCmdBtnAdd(HtmlCommandButton cmdBtnAdd) {
        this.cmdBtnAdd = cmdBtnAdd;
    }

    /**
     * @return the cmdBtnEdit
     */
    public HtmlCommandButton getCmdBtnEdit() {
        return cmdBtnEdit;
    }

    /**
     * @param cmdBtnEdit the cmdBtnEdit to set
     */
    public void setCmdBtnEdit(HtmlCommandButton cmdBtnEdit) {
        this.cmdBtnEdit = cmdBtnEdit;
    }

    /**
     * @return the cmdBtnSave
     */
    public HtmlCommandButton getCmdBtnSave() {
        return cmdBtnSave;
    }

    /**
     * @param cmdBtnSave the cmdBtnSave to set
     */
    public void setCmdBtnSave(HtmlCommandButton cmdBtnSave) {
        this.cmdBtnSave = cmdBtnSave;
    }

    /**
     * @return the cmdBtnDelete
     */
    public HtmlCommandButton getCmdBtnDelete() {
        return cmdBtnDelete;
    }

    /**
     * @param cmdBtnDelete the cmdBtnDelete to set
     */
    public void setCmdBtnDelete(HtmlCommandButton cmdBtnDelete) {
        this.cmdBtnDelete = cmdBtnDelete;
    }

    /**
     * @return the cmdBtnCancel
     */
    public HtmlCommandButton getCmdBtnCancel() {
        return cmdBtnCancel;
    }

    /**
     * @param cmdBtnCancel the cmdBtnCancel to set
     */
    public void setCmdBtnCancel(HtmlCommandButton cmdBtnCancel) {
        this.cmdBtnCancel = cmdBtnCancel;
    }
    
    /**
     * @return the dataTables
     */
    public List<RmsGroup> getDataTables() {
        dataTables = (ArrayList<RmsGroup>) HibernateUtil.createQuery("FROM RmsGroup WHERE groupIsactive = 1");
        return dataTables;
    }

    /**
     * @param dataTables the dataTables to set
     */
    public void setDataTables(List<RmsGroup> dataTables) {
        this.dataTables = dataTables;
    }
}
